When external content is exposed via a data provider, the content is represented by a Sitecore item. Every Sitecore item has an ID. It is likely that the content in the external system also has a unique identifier. Being able to map a Sitecore item ID to the unique identifier in the external system is an important part of building a data provider.
The following are common options for handing this mapping:
Sitecore provides a storage area called the IDTable where external identifiers can be mapped to Sitecore item IDs.
Sitecore has an API that is dedicated to mapping identifiers to Sitecore item IDs. The API is found in the namespace Sitecore.Data.IDTables.
Using the API requires you understand a few concepts:
The following code samples demonstrate how to work with the IDTable API:
var prefix = "source1";
var key = "product1";
var newId = Sitecore.Data.ID.Parse("{5D31DF26-9562-47E0-8095-666BD681AD08}");
var entry = IDTable.Add(prefix, key, newId);
var prefix = "source1";
var key = "product1";
var newId = Sitecore.Data.ID.Parse("{5D31DF26-9562-47E0-8095-666BD681AD08}");
var parentId = Sitecore.Data.ID.Parse("{1E7B9470-06CC-4E59-B8F5-9CD221454E71}");
var entry = IDTable.Add(prefix, key, newId, parentId);
var prefix = "source1";
var key = "product1";
var newId = Sitecore.Data.ID.Parse("{5D31DF26-9562-47E0-8095-666BD681AD08}");
var parentId = Sitecore.Data.ID.Parse("{1E7B9470-06CC-4E59-B8F5-9CD221454E71}");
var customData = "name=Something|place=Here";
var entry = IDTable.Add(prefix, key, newId, parentId, customData);
var prefix = "source1";
var key = "product1";
var entry = IDTable.GetID(prefix, key);
var prefix = "source1";
var id = Sitecore.Data.ID.Parse("{5D31DF26-9562-47E0-8095-666BD681AD08}");
var entry = IDTable.GetKeys(prefix, id).FirstOrDefault();
var prefix = "source1";
var key = "product1";
var entries = IDTable.GetKeys(prefix);
var prefix = "source1";
var key = "product1";
IDTable.RemoveKey(prefix, key);
var prefix = "source1";
var id = Sitecore.Data.ID.Parse("{5D31DF26-9562-47E0-8095-666BD681AD08}");
IDTable.RemoveID(prefix, id);
There is no API for updating an entry, so an entry must be removed and then added.
var prefix = "source1";
var key = "product1";
var id = Sitecore.Data.ID.Parse("{5D31DF26-9562-47E0-8095-666BD681AD08}");
IDTable.RemoveID(prefix, id);
var customData = "name=Something|place=Here";
var entry = IDTable.Add(prefix, key, id, Sitecore.Data.ID.Null, customData);
Sitecore must store the mappings somewhere. This is handled by a provider that inherits from Sitecore.Data.IDTables.IDTableProvider.
The default provider uses a relational database table to store the mappings. This table is named IDTable. The IDTable configuration identifies which relational database is used. By default Sitecore uses the relational database that corresponds to the connection string named master:
<IDTable type="Sitecore.Data.$(database).$(database)IDTable, Sitecore.Kernel" singleInstance="true">
<param connectionStringName="master" />
<param desc="cacheSize">2500KB</param>
</IDTable>
The default provider implements some additional features:
The default IDTable provider uses implements its own caching. The size of the cache is specified in the IDTable configuration.
<IDTable type="Sitecore.Data.$(database).$(database)IDTable, Sitecore.Kernel" singleInstance="true">
<param connectionStringName="master" />
<param desc="cacheSize">2500KB</param>
</IDTable>
The default IDTable provider triggers the following events:
It is possible to generate a GUID using a string. The algorithm is described in RFC4122 from the IETF.
A C# implementation of the algorithm is available on GitHub.
The following demonstrates how to use this implementation. The GUID {9D02E821-E902-5BA1-BE5E-3B3F87F2DB51} will always returned when the external identifier D-7734J is supplied.
var externalId = "D-7734J";
var guid = GuidUtility.Create(GuidUtility.IsoOidNamespace, externalId);
Use the following code to check if a specific GUID corresponds to an external identifier:
var guidExpected = Guid.Parse("{9D02E821-E902-5BA1-BE5E-3B3F87F2DB51}");
var externalId = "D-7734J";
var guidCreated = GuidUtility.Create(GuidUtility.IsoOidNamespace, externalId);
var match = (guidExpected == guidCreated);